home *** CD-ROM | disk | FTP | other *** search
/ Kit PC World De Ampliacion De Windows 95 / Kit PC World de ampliacion de Windows 95.iso / internet / sweeper / samples / surfbear / surfbe~1.cpp < prev    next >
C/C++ Source or Header  |  1995-11-28  |  8KB  |  320 lines

  1. // SurfBearDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "InternetThread.h"
  6. #include "SurfBear.h"
  7. #include "SurfBearDlg.h"
  8.  
  9. #include "AccessTypeDlg.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CAboutDlg dialog used for App About
  19.  
  20. class CAboutDlg : public CDialog
  21. {
  22. public:
  23.     CAboutDlg();
  24.  
  25. // Dialog Data
  26.     //{{AFX_DATA(CAboutDlg)
  27.     enum { IDD = IDD_ABOUTBOX };
  28.     //}}AFX_DATA
  29.  
  30.     // ClassWizard generated virtual function overrides
  31.     //{{AFX_VIRTUAL(CAboutDlg)
  32.     protected:
  33.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  34.     //}}AFX_VIRTUAL
  35.  
  36. // Implementation
  37. protected:
  38.     //{{AFX_MSG(CAboutDlg)
  39.     //}}AFX_MSG
  40.     DECLARE_MESSAGE_MAP()
  41. };
  42.  
  43. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  44. {
  45.     //{{AFX_DATA_INIT(CAboutDlg)
  46.     //}}AFX_DATA_INIT
  47. }
  48.  
  49. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  50. {
  51.     CDialog::DoDataExchange(pDX);
  52.     //{{AFX_DATA_MAP(CAboutDlg)
  53.     //}}AFX_DATA_MAP
  54. }
  55.  
  56. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  57.     //{{AFX_MSG_MAP(CAboutDlg)
  58.         // No message handlers
  59.     //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CSurfBearDlg dialog
  64.  
  65. CSurfBearDlg::CSurfBearDlg(CWnd* pParent /*=NULL*/)
  66.     : CDialog(CSurfBearDlg::IDD, pParent)
  67. {
  68.     //{{AFX_DATA_INIT(CSurfBearDlg)
  69.         // NOTE: the ClassWizard will add member initialization here
  70.     //}}AFX_DATA_INIT
  71.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  72.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  73. }
  74.  
  75. void CSurfBearDlg::DoDataExchange(CDataExchange* pDX)
  76. {
  77.     CDialog::DoDataExchange(pDX);
  78.     //{{AFX_DATA_MAP(CSurfBearDlg)
  79.     DDX_Control(pDX, IDC_BTN_ACCESS, m_btnAccess);
  80.     DDX_Control(pDX, IDOK, m_btnGoto);
  81.     DDX_Control(pDX, IDCANCEL, m_btnClose);
  82.     DDX_Control(pDX, IDC_EDIT_BROWSE, m_editBrowse);
  83.     DDX_Control(pDX, IDC_EDIT_ADDRESS, m_editAddress);
  84.     DDX_Control(pDX, IDC_ANIMATE, m_ctrlAnimate);
  85.     //}}AFX_DATA_MAP
  86. }
  87.  
  88. BEGIN_MESSAGE_MAP(CSurfBearDlg, CDialog)
  89.     //{{AFX_MSG_MAP(CSurfBearDlg)
  90.     ON_WM_SYSCOMMAND()
  91.     ON_WM_PAINT()
  92.     ON_WM_QUERYDRAGICON()
  93.     ON_BN_CLICKED(IDOK, OnGotoBtn)
  94.     ON_WM_SIZE()
  95.    ON_MESSAGE(WM_READFILECOMPLETED, OnReadFileCompleted) 
  96.     ON_BN_CLICKED(IDC_BTN_ACCESS, OnBtnAccess)
  97.       ON_COMMAND(IDC_HELPIMAGE, OnHelpAbout)
  98.     //}}AFX_MSG_MAP
  99. END_MESSAGE_MAP()
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CSurfBearDlg message handlers
  103.  
  104. BOOL CSurfBearDlg::OnInitDialog()
  105. {
  106.     CDialog::OnInitDialog();
  107.  
  108.     // Add "About..." menu item to system menu.
  109.  
  110.     // IDM_ABOUTBOX must be in the system command range.
  111.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  112.     ASSERT(IDM_ABOUTBOX < 0xF000);
  113.  
  114.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  115.     CString strAboutMenu;
  116.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  117.     if (!strAboutMenu.IsEmpty())
  118.     {
  119.         pSysMenu->AppendMenu(MF_SEPARATOR);
  120.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  121.     }
  122.  
  123.     // Set the icon for this dialog.  The framework does this automatically
  124.     //  when the application's main window is not a dialog
  125.     SetIcon(m_hIcon, TRUE);            // Set big icon
  126.     SetIcon(m_hIcon, FALSE);        // Set small icon
  127.     
  128.     // TODO: Add extra initialization here
  129.  
  130.    // Initialize the Browse Edit Control
  131.    CRect rectClient ;
  132.    GetClientRect(&rectClient) ;
  133.    SetEditBrowseSize(rectClient.Width(), rectClient.Height()) ;
  134.  
  135.    // Initialize the Address Edit Control
  136.    m_editAddress.SetWindowText("http://www.microsoft.com") ;
  137.  
  138.    // Initialize the InternetThread
  139.    m_InternetThread.Init(GetSafeHwnd()) ;
  140.  
  141.    // Initialize Animation Control
  142.    m_ctrlAnimate.Open(IDR_AVI_BOOKS) ;
  143.  
  144.    // Initialize the Help Bitmap Button
  145.    m_btnBitmapHelp.AutoLoad(IDC_HELPIMAGE, this) ;
  146.  
  147.    return TRUE;  // return TRUE  unless you set the focus to a control
  148. }
  149.  
  150. void CSurfBearDlg::OnSysCommand(UINT nID, LPARAM lParam)
  151. {
  152.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  153.     {
  154.         CAboutDlg dlgAbout;
  155.         dlgAbout.DoModal();
  156.     }
  157.     else
  158.     {
  159.         CDialog::OnSysCommand(nID, lParam);
  160.     }
  161. }
  162.  
  163. // If you add a minimize button to your dialog, you will need the code below
  164. //  to draw the icon.  For MFC applications using the document/view model,
  165. //  this is automatically done for you by the framework.
  166.  
  167. void CSurfBearDlg::OnPaint() 
  168. {
  169.     if (IsIconic())
  170.     {
  171.         CPaintDC dc(this); // device context for painting
  172.  
  173.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  174.  
  175.         // Center icon in client rectangle
  176.         int cxIcon = GetSystemMetrics(SM_CXICON);
  177.         int cyIcon = GetSystemMetrics(SM_CYICON);
  178.         CRect rect;
  179.         GetClientRect(&rect);
  180.         int x = (rect.Width() - cxIcon + 1) / 2;
  181.         int y = (rect.Height() - cyIcon + 1) / 2;
  182.  
  183.         // Draw the icon
  184.         dc.DrawIcon(x, y, m_hIcon);
  185.     }
  186.     else
  187.     {
  188.         CDialog::OnPaint();
  189.     }
  190. }
  191.  
  192. // The system calls this to obtain the cursor to display while the user drags
  193. //  the minimized window.
  194. HCURSOR CSurfBearDlg::OnQueryDragIcon()
  195. {
  196.     return (HCURSOR) m_hIcon;
  197. }
  198.  
  199. //
  200. // User has pressed the Goto Button. Start worker thread.
  201. //
  202. void CSurfBearDlg::OnGotoBtn() 
  203. {
  204.    TRACE0("OnGotoBtn\r\n") ;
  205.  
  206.    CString strAddress ;
  207.    m_editAddress.GetWindowText(strAddress) ;
  208.    if (strAddress.IsEmpty()
  209.       || "http://" != strAddress.Left(7))
  210.    {
  211.       AfxMessageBox("Please, http address required.", MB_OK | MB_ICONINFORMATION) ;
  212.       return ;
  213.    }
  214.  
  215.    //
  216.    // Disable the UI
  217.    //
  218.    EnableUI(FALSE) ;
  219.  
  220.    //
  221.    // Get the thread to get a page.
  222.    //
  223.    m_InternetThread.GetPage(strAddress) ;
  224.  
  225. }
  226.  
  227. //
  228. // OnSize
  229. //
  230. void CSurfBearDlg::OnSize(UINT nType, int cx, int cy) 
  231. {
  232.     CDialog::OnSize(nType, cx, cy);
  233.    if (nType == SIZE_MAXIMIZED || nType == SIZE_RESTORED)     
  234.       SetEditBrowseSize(cx, cy) ;   
  235. }
  236.  
  237.  
  238. //
  239. // Enable or disable the controls .
  240. //
  241. void CSurfBearDlg::EnableUI(BOOL bEnable)
  242. {
  243.    m_btnGoto.EnableWindow(bEnable) ;
  244.    m_editAddress.EnableWindow(bEnable) ;
  245.    m_btnAccess.EnableWindow(bEnable); 
  246.  
  247.    if (bEnable)
  248.    {
  249.       m_ctrlAnimate.Stop() ;
  250.       m_ctrlAnimate.Seek(0) ;
  251.    }
  252.    else
  253.    {
  254.       m_ctrlAnimate.Play(0, (UINT)-1, (UINT)-1) ;
  255.    }
  256. }
  257.  
  258. //
  259. // Set the size of the edit control which displays the raw HTML.
  260. //
  261. void CSurfBearDlg::SetEditBrowseSize(int cx, int cy)
  262. {
  263.    if (cx > 0 && cy >0)
  264.    {
  265.       CRect aRect ;
  266.       if (m_editBrowse.m_hWnd != NULL)
  267.       {
  268.          m_editBrowse.GetWindowRect(aRect) ;
  269.          ScreenToClient(aRect) ;
  270.           m_editBrowse.MoveWindow(0, aRect.top,
  271.                                  cx, cy-aRect.top) ;
  272.       }
  273.    }
  274. }
  275.  
  276. //
  277. // OnReadFileCompleted -   Internet Thread sends a user defined WM_READFILECOMPLETED
  278. //                         message to this window, when the thread has finished.
  279. //
  280. afx_msg LRESULT CSurfBearDlg::OnReadFileCompleted(WPARAM wParam, LPARAM lParam)
  281. {
  282.    TRACE0("CSurfBearDlg::OnReadFileCompleted\r\n") ;
  283.    UINT uiResult = (UINT)lParam ;
  284.    if ( uiResult == THREAD_GOOD)
  285.    {
  286.       m_editBrowse.SetWindowText(m_InternetThread.GetBuffer()) ;
  287.       m_InternetThread.EmptyBuffer() ;
  288.    }
  289.    
  290.    EnableUI(TRUE) ;
  291.  
  292.    return TRUE ;
  293. }
  294.  
  295. //
  296. // Display dialog box which allows changing the access type.
  297. //
  298. void CSurfBearDlg::OnBtnAccess() 
  299. {
  300.     CAccessTypeDlg aDlg ;
  301.  
  302.    aDlg.m_strProxyServer = m_InternetThread.GetProxyServer() ;
  303.    aDlg.m_iRadioBtns = m_InternetThread.GetAccessTypeIndex() ;
  304.  
  305.    if (aDlg.DoModal() == IDOK)
  306.    {
  307.       m_InternetThread.SetProxyServer(aDlg.m_strProxyServer) ;
  308.       m_InternetThread.SetAccessTypeIndex(aDlg.m_iRadioBtns); 
  309.    }
  310.     
  311. }
  312.  
  313. //
  314. //
  315. //
  316. void CSurfBearDlg::OnHelpAbout()
  317. {
  318.    CAboutDlg aDlg ;
  319.    aDlg.DoModal() ;
  320. }